import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)
const persist = (store) => {
  // console.log("我的插件生效了")
  // console.log(store);
  //每次调用mutations时会执行subscribe函数
  store.subscribe((mutations, state) => {
    // console.log(mutations)
    // console.log(state)
    const data = JSON.stringify(state);
    window.sessionStorage.setItem('vuexState', data)
  })
}
export default new Vuex.Store({
  plugins: [persist],
  state: JSON.parse(window.sessionStorage.getItem('vuexState')) || {
    myMoney: 380,
    carMoney:300
  },
  mutations: {
    chong(state, data) {
      state.myMoney += data
    },
    ti(state, data) {
      state.carMoney -= data
    }
  },
  actions: {},
  modules: {}
})